package cn.littlehx.spring_boot_test2_2.service;

import cn.littlehx.spring_boot_test2_2.domain.Teacher;
import cn.littlehx.spring_boot_test2_2.repository.StudentRepository;
import cn.littlehx.spring_boot_test2_2.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;

    /**
     * 查询所有老师
     *
     * @return
     */
    public List<Teacher> findAll() {
        return teacherRepository.findAll();
    }

    /**
     * 按照id进行查询
     *
     * @param id
     * @return
     */
    public Teacher findById(Integer id) {
        return teacherRepository.findById(id).orElse(null);
    }

    /**
     * 按照名字进行查询
     *
     * @return
     */
    public List<Teacher> findByNameLike(String name) {
        return teacherRepository.findByNameLike("%" + name + "%");
    }

    /**
     * 按照名字和密码进行查询
     *
     * @param name
     * @param password
     * @return
     */
    public List<Teacher> findByNameAndPassword(String name, String password) {
        return teacherRepository.findByNameAndPassword(name, password);
    }

    /**
     * 插入新的老师
     *
     * @param teacher
     */
    public void insert(Teacher teacher) {
        teacherRepository.save(teacher);
    }

    /**
     * 修改老师
     *
     * @param teacher
     */
    public void update(Teacher teacher) {
        teacherRepository.save(teacher);
    }

    /**
     * 通过id进行删除
     *
     * @param id
     */
    public void deleteById(Integer id) {
        teacherRepository.deleteById(id);
    }

    public Teacher getMaxByNo(){
        return teacherRepository.findAll(Sort.by("no").descending()).get(0);
    }


}
